package com.yyyit.airmode;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.yyyit.common.HttpRequest;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button bt1;
    private Button bt2;
    private ListView lv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new bt1Listener(1));
        bt1 = (Button) findViewById(R.id.button);
        bt1.setOnClickListener(new bt1Listener(1));
        bt2 = (Button) findViewById(R.id.button1);
        bt2.setOnClickListener(new bt1Listener(2));
        lv1 = (ListView) findViewById(R.id.listView);



        List<String> data = new ArrayList<String>();
        data.add("测试数据1");
        data.add("测试数据2");
        data.add("测试数据3");
        data.add("测试数据4");


        ArrayAdapter aad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
        lv1.setAdapter(aad);
        data.add("测试数据4");
        data.add("测试数据4");
        data.add("测试数据4");
        data.add("测试数据4");
        data.add("测试数据4");
        aad.notifyDataSetChanged();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    private class bt1Listener implements View.OnClickListener {
        private int a;

        public bt1Listener(int a) {
            super();
            this.a = a;
        }

        public void onClick(View v) {
            if (a == 1) {
                Uri uri = Uri.parse("market://details?id=com.qding.community");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } else if (a == 2) {
                HttpRequest ht = new HttpRequest();
                String st = ht.HttpRequest("http://www.baidu.com");
                bt2.setText(st);
            }


        }
    }


}
